home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / x / gui / xfract.lha / xfract / xpt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-27  |  4.6 KB  |  190 lines

  1. /*****************************************************************************
  2. /* FILE        : xpt.c
  3. /* DATE        : August 25, 1989.
  4. /* AUTHOR    : Paul Sharpe @ DEC, Reading (OSCR-Europe).
  5. /* FUNCTION    : 'toolkit' of common functions.
  6. /*
  7. /*   Copyright (c) Digital Equipment Corporation 1990  All rights reserved.
  8. /*   Copyright is claimed in the computer program and user interface thereof.
  9. /*
  10. /*   Digital Equipment Corporation cannot accept any responsibility for
  11. /*   use, misuse, or abuse of this software.
  12. /*
  13. /*****************************************************************************/
  14.  
  15. #include <stdio.h>
  16.  
  17. #include <X11/Xlib.h>
  18. #include <X11/Xatom.h>
  19. #include <X11/Xutil.h>
  20.  
  21. #include "xpt.h"
  22.  
  23. char    *prog;
  24.  
  25. Display *
  26. xpt_open_display(str)
  27. char    *str;
  28. {
  29. extern char    *getenv();
  30. Display        *dsply;
  31.  
  32.     if (str == (char *)NULL || *str == '\0')
  33.     if ((str = getenv("DISPLAY")) == NULL)
  34.         str = DEFDSPLY;
  35.     dsply = XOpenDisplay(str);
  36.     if (dsply == (Display *)NULL) {
  37.     fprintf(stderr,"Sorry - can't open display '%s'\n",str);
  38.     exit(1);
  39.     }
  40.     return(dsply);
  41. }
  42.  
  43. xpt_getargs(args, numargs, argc,argv)
  44. struct    opts    args[];
  45. int        numargs;
  46. int        argc;
  47. char        *argv[];
  48. {
  49. int    i, gotflag = 0;
  50.  
  51.     prog = *argv;
  52.     while (--argc > 0) {
  53.     gotflag = 0;
  54.     if (**++argv != '-')
  55.         xpt_usage(args,numargs,"Value with no flag: %s",*argv);
  56.     for (i=0; i<numargs; i++) {
  57.         if (strncmp(*argv,args[i].flagstr,strlen(*argv))==0 ||
  58.             strcmp(*argv,args[i].aflagstr)==0) {
  59.  
  60. /* Have we already had a match for this argument string ? */
  61.         if (gotflag == 1)
  62.             xpt_usage(args,numargs,"Ambiguous flag: %s",*argv);
  63.         if (args[i].valstr != (char *)TRUE && args[i].valstr != (char *)FALSE) {
  64. /* We want an argument value from the next argv component. */
  65.             ++argv;
  66.             if (--argc <= 0)
  67.             xpt_usage(args,numargs,"Flag with no value: %s",*argv);
  68.             args[i].valstr = *argv;
  69.         }
  70.         gotflag = 1;
  71.         }
  72.     }
  73.     if (gotflag == 0)
  74.         xpt_usage(args,numargs,"Unknown flag: %s",*argv);
  75.     }
  76. }
  77.  
  78. xpt_usage(args,numargs, fmt,str)
  79. struct opts    args[];
  80. int        numargs;
  81. char        *fmt, *str;
  82. {
  83. int    i;
  84.  
  85.     fprintf(stderr,fmt,str);
  86.     fprintf(stderr,"\nUsage: %s \n",prog);
  87.     for (i=0; i<numargs; i++)
  88.     fprintf(stderr,"           %s\n",args[i].usage);
  89.     exit(1);
  90. }
  91.  
  92. Window
  93. xpt_window(dsply,parent,x,y,sx,sy,mask)
  94. Display    *dsply;
  95. Window    parent;
  96. int    x,y,sx,sy,mask;
  97. {
  98. Window            new;
  99. XSetWindowAttributes    attrs;
  100.  
  101.     attrs.event_mask = mask | ExposureMask;
  102.  
  103.     new = XCreateWindow(dsply,parent,x,y,sx,sy,2,DefaultDepth(dsply,DefaultScreen(dsply)),InputOutput,CopyFromParent,CWEventMask,&attrs);
  104.     if (new == (Window)NULL) {
  105.     fprintf(stderr,"FATAL: Couldn't create window.\n");
  106.     exit(1);
  107.     }
  108.     return(new);
  109. }
  110.  
  111. xpt_colour(dsply,name,clmap)
  112. Display        *dsply;
  113. char         *name;
  114. Colormap    clmap;
  115. {
  116. XColor        colour;
  117. static Colormap    defclmap = NULL;
  118.  
  119.     if (defclmap == (Colormap)NULL)
  120.     defclmap = DefaultColormapOfScreen(DefaultScreenOfDisplay(dsply));
  121.     if (clmap == (Colormap)NULL)
  122.     clmap = defclmap;
  123.  
  124.     if (XParseColor(dsply,clmap,name,&colour) == BadColor) {
  125.     fprintf(stderr,"WARNING: Failed to parse colour '%s'...\n",name);
  126.     return(0);
  127.     }
  128.     if (XAllocColor(dsply,clmap,&colour) == 0) {
  129.     fprintf(stderr,"FATAL: Failed to alloc colour '%s'...\n",name);
  130.     exit(1);
  131.     }
  132.     return(colour.pixel);
  133. }
  134.  
  135. xpt_windowcols(dsply,wndw,gc,clmap,bg,bd,tile)
  136. Display        *dsply;
  137. Window        wndw;
  138. GC        gc;
  139. Colormap    clmap;
  140. char        *bg, *bd, *tile;
  141. {
  142.     if (bg != (char *)NULL && *bg != '\0');
  143.     XSetWindowBackground(dsply,wndw,xpt_colour(dsply,bg,clmap));
  144.     if (bd != (char *)NULL && *bd != '\0');
  145.     XSetWindowBorder(dsply,wndw,xpt_colour(dsply,bd,clmap));
  146.     xpt_tile(dsply,wndw,gc,tile);
  147. }
  148.  
  149. /* Read a bitmap: if colour, convert to a pixmap; set to window background. */
  150. xpt_tile(dsply,wndw,gc,file)
  151. Display    *dsply;
  152. Window    wndw;
  153. GC    gc;
  154. char    *file;
  155. {
  156. int    h,w,x,y, depth;
  157. Pixmap    b, pix;
  158.  
  159.     if (file == (char *)NULL || *file == '\0')
  160.     return;
  161.     if (XReadBitmapFile(dsply,wndw,file,&w,&h,&b,&x,&y) != BitmapSuccess) {
  162.     fprintf(stderr,"WARNING: Can't load background bitmap '%s'.\n",file);
  163.     return;
  164.     }
  165.     if ((depth = DefaultDepth(dsply,DefaultScreen(dsply))) == 1)
  166.     XSetWindowBackgroundPixmap(dsply,wndw,b);
  167.     else {
  168.     pix = XCreatePixmap(dsply,wndw,w,h,depth);
  169.     XCopyPlane(dsply,b,pix,gc,0,0,w,h,0,0,1);
  170.     XSetWindowBackgroundPixmap(dsply,wndw,pix);
  171.     XFreePixmap(dsply,pix);
  172.     }
  173. }
  174.  
  175. xpt_icon(dsply,wndw,title,iconfile)
  176. Display    *dsply;
  177. Window    wndw;
  178. char    *title, *iconfile;
  179. {
  180. Pixmap    b;
  181. int    h,w,x,y;
  182. char    name[1][256];
  183.  
  184.     strcpy(name[0],title);
  185.     if (XReadBitmapFile(dsply,wndw,iconfile,&w,&h,&b,&x,&y) != BitmapSuccess)
  186.     fprintf(stderr,"WARNING: Can't load icon bitmap '%s'.\n",iconfile);
  187.     else
  188.     XSetStandardProperties(dsply,wndw,*name,*name,b,name,0,NULL);
  189. }
  190.